package com.mediacloud.app.newsmodule.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mediacloud.app.assembly.util.Utility;
import com.mediacloud.app.model.adaptor.BaseAdaptor;
import com.mediacloud.app.newsmodule.R;

/**
 * 音频下面的那个列表
 * @author x
 *
 * @param <T>
 */
public class AudioBottomPopWinX<T> extends PopupWindow
{
	public static final int StreamMode=1;
	public static final int AudioListMode=2;

	public int mode=StreamMode;
	protected View contentView;
	protected Context mContext;
	protected OnItemClickListener listener;
	protected BaseAdaptor<T> adaptor;
    public ListView audio_bottom_list;
    public TextView tvPopTitle;
	public AudioBottomPopWinX(Context context)
	{
		super(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		setBackgroundDrawable(new BitmapDrawable());
		setOutsideTouchable(true);
		mContext=context;
		contentView=LayoutInflater.from(mContext).inflate(R.layout.aappfactory_aduio_streamx, null);
		setContentView(contentView);
		contentView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				boolean b= contentView.onTouchEvent(event);
				dismiss();
				return b;
			}
		});
		tvPopTitle=Utility.findViewById(contentView, R.id.tv_pop_title);
		audio_bottom_list=Utility.findViewById(contentView, R.id.audio_bottom_list);
//		audio_bottom_list.setPullLoadEnable(false);
//		audio_bottom_list.setPullRefreshEnable(false);
		audio_bottom_list.setHeaderDividersEnabled(false);
		audio_bottom_list.setDivider(null);
		audio_bottom_list.setFooterDividersEnabled(false);
		audio_bottom_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				dismiss();
				if(listener!=null)
				{
					listener.onItemClick(parent, view, position, id);
				}
			}
		});
        ImageView imageViewClose = Utility.findViewById(contentView, R.id.image_close);
        imageViewClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
		setAnimationStyle(R.style.BottomDialog_Animation2);
	}

	public void setPopTitle(String title){
		tvPopTitle.setText(title);
	}
	public void setItemClickListener(OnItemClickListener listener)
	{
		this.listener=listener;
	}
	
	public void setListAdaptor(BaseAdaptor<T> adaptor)
	{
		this.adaptor=adaptor;
		audio_bottom_list.setAdapter(this.adaptor);
	}
	public void show(final View view)
	{
		dismiss();
		int heightPixels=mContext.getResources().getDisplayMetrics().heightPixels;
		if(contentView.getHeight()<=0)
		{
			contentView.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
				@Override
				public boolean onPreDraw() {
					contentView.getViewTreeObserver().removeOnPreDrawListener(this);
					dismiss();
					int heightPixels=mContext.getResources().getDisplayMetrics().heightPixels;
					showAtLocation(view, Gravity.NO_GRAVITY,0, heightPixels-contentView.getHeight());
					return true;
				}
			});
		}
		showAtLocation(view, Gravity.NO_GRAVITY,0, heightPixels-contentView.getHeight());
	}

    public void show(View view, boolean showTitle) {
        contentView.findViewById(R.id.frame_title).setVisibility(showTitle ? View.VISIBLE : View.GONE);
        show(view);
    }
}